
Ext.onReady(function() {
	
	$('#user_logout').click(function() {		//注销用户
		$('#user_logout').text('正在注销...');
		
		Ext.Ajax.request({
			url: '/szpxt-beta/user_logout',
			success: function(response, opts) {
				var obj = Ext.decode(response.responseText);
				if(obj.success) {
					document.location = '/szpxt-beta/main/home.jsp';
				}
			}
		});
	});
	
	new Ext.tree.TreePanel({
		id: 'extTreePanel',
		root: {id: '-1', text: '部门结构', expanded: true},
		dataUrl: '/szpxt-beta/user_getOrganizationTree',
		baseCls: 'x-plain',
		lines: false,
		renderTo: 'treePanel',
		listeners: {
			click: function(node) {		//设置归属机构
				if(Ext.getCmp('extForm')) {
					Ext.getCmp('organizationId').setValue(node.id);
					Ext.getCmp('organizationName').setValue(node.text);
				}
				else if(Ext.getCmp('gridPanel')) {			//刷新用户列表
					Ext.getCmp('gridPanel').getStore().load({params: {organizationId: node.id, start: 0, limit: 20}})
				}
				
				$('.messageRightTop').hide();
				$('.messageFalseTop').hide();
			}}
	});
	
	//新增和列表按钮
	$('.add, .buttonList, .display, .submit, .clearForm').append('<span class="hover"></span>').each(function() {
		var $span = $('> span.hover', this).css('opacity', 0);
		$(this).hover(function() {
			$span.stop().fadeTo(500, 1);
		}, function() {
			$span.stop().fadeTo(500, 0);
		})
	});
	
	//点击事件
	$('.add, .buttonList').click(function() {
		$('.messageRight').hide();
		$('.messageFalse').hide();
		$('.messageRightTop').hide();
		$('.messageFalseTop').hide();
		$('#form').empty();		//先清空
		$('#list').empty();
		
		if(this.className == 'buttonList') {		//用户列表检查
			var selectedNode = Ext.getCmp('extTreePanel').getSelectionModel().getSelectedNode();	//获取的节点
			if(!selectedNode) {
				$('.messageFalseTop').show();
				$('.messageFalseTextTop').text('请选择需要显示用户列表的组织');
				return;
			}
		}
		
		$('#button').hide('slow');
		$('#buttonDisplay').show('slow');
		
		if(this.className == 'add') {
			$('#bigForm').show('slow');
			showForm();				//后装载
		}
		else if(this.className == 'buttonList') {		//用户列表
			var selectedNode = Ext.getCmp('extTreePanel').getSelectionModel().getSelectedNode();	//获取的节点
			
			$('#bigList').show('slow');
			showList(selectedNode.id);
		}
	});
	
	$('.display').click(function() {
		$('.messageRightTop').hide('slow');
		$('.messageFalseTop').hide('slow');
		$('#buttonDisplay').hide('slow');
		$('#bigForm').hide('slow');
		$('#bigList').hide('slow');
		
		$('#button').show('slow');
	});
	
	$('.submit').click(function() {		//提交保存
		if(Ext.getCmp('extForm').getForm().isValid()) {
			//检验确认密码
			var password = Ext.getCmp('password');
			var confirmPassword = Ext.getCmp('confirmPassword');
			if(password && confirmPassword && password.getValue() != confirmPassword.getValue()) {
				$('.messageRight').hide();
				$('.messageFalse').show();
				$('.messageFalseText').text('输入的"密码"和"确认密码"不相同，请重新输入');
				
				password.setValue('');
				confirmPassword.setValue('');
				
				return;
			}
			
			
			Ext.getCmp('extForm').getForm().submit({
				url: '/szpxt-beta/user_saveOrUpdate',
				success: function(form, action) {
					$('.messageFalse').hide();		//提示信息
					$('.messageRight').show();
					$('.messageRightText').text('"' + action.result.data.name + '"保存成功');
				},
				failure: function() {
					$('.messageRight').hide();		//提示信息
					$('.messageFalse').show();
					$('.messageFalseText').text('保存失败');
				}
			});
		}
	})
	
	$('.clearForm').click(function() {
		Ext.getCmp('extForm').getForm().reset();	//清空
		Ext.getCmp('organizationName').setValue('');
		Ext.getCmp('extForm').getForm().clearInvalid();
	})
	
	$('.messageRight, .messageFalse, .messageRightTop, .messageFalseTop').each(function() {
		$(this).click(function() {
			$(this).hide();
		})
	})
	
})

//显示表格
function showForm() {
	new Ext.form.FormPanel({
		id: 'extForm',
		baseCls: 'x-plain',
		width: 400,
		labelAlign: 'right',
		defaults: {xtype: 'textfield', width: 200, allowBlank: false},
		items: [
			{fieldLabel: '名称', name: 'name'},
			{fieldLabel: '密码', id: 'password', name: 'password', inputType: 'password'},
			{fieldLabel: '密码确认', id: 'confirmPassword', inputType: 'password'},
			{fieldLabel: '所属机构', name: 'organizationName', id: 'organizationName', readOnly: true, emptyText: '请左侧选择组织机构'},
			{xtype: 'combo', fieldLabel: '角色', hiddenName: 'roleId', displayField: 'name', valueField: 'id',  
				store: new Ext.data.JsonStore({
					autoLoad: true,
					url: '/szpxt-beta/user_getAllRole',
					root: 'data',
					fields: ['id', 'name']
			}), triggerAction: 'all'},
			{xtype: 'hidden', name: 'organizationId', id: 'organizationId'},
			{xtype: 'hidden', name: 'id'}
		],
		renderTo: 'form',
		listeners: {
			afterrender: function() {
				var selectedNode = Ext.getCmp('extTreePanel').getSelectionModel().getSelectedNode();	//设置归属机构
				if(selectedNode) {
					Ext.getCmp('organizationId').setValue(selectedNode.id);
					Ext.getCmp('organizationName').setValue(selectedNode.text);
		}}}
	});
}

function loadForm(id) {
	var extForm = Ext.getCmp('extForm');
	extForm.getForm().load({				//读数据
		url: '/szpxt-beta/user_get',
		params: {id: id}
	});
	extForm.remove('password');				//删除密码修改
	extForm.remove('confirmPassword');
	
	var radiogroup = {xtype: 'radiogroup', fieldLabel: '是否修改密码', items: [
		{boxLabel: '是', name: 'choicePassword', listeners: {check: function(radio, checked) {
			if(checked) {
				var password = {fieldLabel: '密码', id: 'password', name: 'password', inputType: 'password'};		//增加密码修改
				var confirmPassword = {fieldLabel: '密码确认', id: 'confirmPassword', inputType: 'password'};
				
				extForm.insert(2, password);
				extForm.insert(3, confirmPassword);
				extForm.doLayout();
			}
		}}},
		{boxLabel: '否', name: 'choicePassword', checked: true, listeners: {check: function(radio, checked) {
			if(checked) {
				extForm.remove('password');				//删除密码修改
				extForm.remove('confirmPassword');
			}
		}}}
	]};
	extForm.insert(1, radiogroup);
	extForm.doLayout();
	
}

function showList(aOrganizationId) {
	var jsonStore = new Ext.data.JsonStore({			//数据源
		url: '/szpxt-beta/user_list',
		baseParams: {start: 0, limit: 20, organizationId: aOrganizationId},
		autoLoad: true,
		root: 'data',
		totalProperty: 'totalCount',
		fields: ['id', 'name']		
	});
	
	var topToolbar = [
		{xtype: 'button', text: '修改', iconCls: 'listModify', listeners: {
			click: function() {
				var selected = Ext.getCmp('gridPanel').getSelectionModel().getSelected();
				if(!selected) {
					$('.messageFalse').show();
					$('.messageFalseText').text('请选择用户');
					return;
				}
				$('#bigList').hide('slow');		//先清除
				
				$('#bigForm').show('slow');		//后显示
				$('#form').empty();	
				showForm();
				loadForm(selected.id);
			}
		}}, '-',
		{xtype: 'button', text: '删除', iconCls: 'listDelete', listeners: {
			click: function() {
				var record = gridPanel.getSelectionModel().getSelected();
				if(!record) {
					$('.messageRight').hide();
					$('.messageFalse').show();
					$('.messageFalseText').text('请选择需要删除的用户');
					return;
				}
				
				var tbar = gridPanel.getTopToolbar();
				
				var buttonConfirm = {xtype: 'button', text: '确认删除', iconCls: 'listConfirm', listeners: {
					click: function() {
						Ext.Ajax.request({
							url: '/szpxt-beta/user_delete',
							params: {id: record.id},
							success: function(response, opts) {
								var obj = Ext.decode(response.responseText);
								if(obj.success) {
									$('.messageFalse').hide();
									$('.messageRight').show();
									$('.messageRightText').text(record.data.name + '删除成功');
									
									tbar.remove(4);
				 					tbar.remove(3);
									
									gridPanel.getStore().reload();
								}
								else {
									$('.messageRight').hide();
									$('.messageFalse').show();
									$('.messageFalseText').text(record.name + '删除失败');
								}
							},
							failure: function() {
								$('.messageRight').hide();
								$('.messageFalse').show();
								$('.messageFalseText').text(record.name + '删除失败');
							}
						});
					}
				}};
				var buttonCancel = {xtype: 'button', text: '取消删除', iconCls: 'listCancel', listeners: {
					click: function() {
				 		tbar.remove(4);
				 		tbar.remove(3);
				 		
				 		$('.messageFalse').hide();
				 		$('.messageRight').hide();
					}	
				}};
				
				tbar.insert(3, buttonConfirm);
				tbar.insert(4, buttonCancel);
				tbar.doLayout();
			}
		}},
		'->', '查询:',
		new SZPXT_Search()
	];
	
	var pagingToolbar = new Ext.PagingToolbar({			//分页
		store: jsonStore,
		pageSize: 20,
		displayInfo: true
	});
	
	var gridPanel = new Ext.grid.GridPanel({							//列表
		id: 'gridPanel',
		store: jsonStore,
		columns: [
			{header: '用户名称', dataIndex: 'name', id: 'listName'}
		],
		autoExpandColumn: 'listName',
		tbar: topToolbar,
		bbar: pagingToolbar,
		id: 'gridPanel',
		title: '用户列表',
		width: 500,
		height: 400,
		renderTo: 'list'
	});
}

//搜索框
SZPXT_Search = Ext.extend(Ext.form.TwinTriggerField, {
	initComponent: function() {
		Ext.apply(this, {
			validationEvent: false,
			validateOnBlur: false,
			trigger1Class: 'x-form-clear-trigger',
			trigger2Class: 'x-form-search-trigger',
			hideTrigger1: true,
			onTrigger1Click: function() {
				var selectedNode = Ext.getCmp('extTreePanel').getSelectionModel().getSelectedNode();	//设置归属机构
				Ext.getCmp('gridPanel').getStore().load({params: {organizationId: selectedNode.id, start: 0, limit: 20}})
				
				this.setValue('');
				this.triggers[0].hide();
			},
			onTrigger2Click: function() {
				var aQuery = this.getValue();
				var selectedNode = Ext.getCmp('extTreePanel').getSelectionModel().getSelectedNode();	//设置归属机构
				Ext.getCmp('gridPanel').getStore().load({params: {organizationId: selectedNode.id, start: 0, limit: 20, query: aQuery}})
				
				this.triggers[0].show();
			},
			width: 180,
			emptyText: '请输入用户名称'
		});
		
		SZPXT_Search.superclass.initComponent.call(this);
	}
})
